package me.chanjar.weixin.cp.tp.service.impl;

import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.GET_CONTACT_DETAIL;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.GET_CONTACT_DETAIL_BATCH;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.ExternalContact.GROUP_CHAT_INFO;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.APPLY_EVENT;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.external.WxCpUserExternalGroupChatInfo;
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactBatchInfo;
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
import me.chanjar.weixin.cp.tp.service.WxCpTpExternalContactService;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

/**
 * @ClassName WxCpTpExternalContactServiceImpl
 * @Description 企业外部联系人
 * @Author 艾成林
 * @Version 2023/03/06 10:19
 **/
@RequiredArgsConstructor
public class WxCpTpExternalContactServiceImpl implements WxCpTpExternalContactService {
  private final WxCpTpService mainService;


  @Override
  public WxCpUserExternalGroupChatInfo getGroupChat(String corpId  ,String chatId, Integer needName) throws WxErrorException {
    JsonObject json = new JsonObject();
    json.addProperty("chat_id", chatId);
    json.addProperty("need_name", needName);
    String url = this.mainService.getWxCpTpConfigStorage().getApiUrl(GROUP_CHAT_INFO) +
      "?access_token=" + mainService.getWxCpTpConfigStorage().getAccessToken(corpId);
    return WxCpUserExternalGroupChatInfo.fromJson(this.mainService.post(url, json.toString()));
  }

  @Override
  public WxCpExternalContactInfo getContactDetail(String corpId,String userId, String cursor) throws WxErrorException {
    String params = userId;
    if (StringUtils.isNotEmpty(cursor)) {
      params = params + "&cursor=" + cursor;
    }
    String url = this.mainService.getWxCpTpConfigStorage().getApiUrl(GET_CONTACT_DETAIL+params) +
      "&access_token=" + mainService.getWxCpTpConfigStorage().getAccessToken(corpId);
    return WxCpExternalContactInfo.fromJson(this.mainService.get(url, null));
  }

  @Override
  public WxCpExternalContactBatchInfo getContactDetailBatch(String corpId, String[] userIdList, String cursor, Integer limit) throws WxErrorException {
    String url = this.mainService.getWxCpTpConfigStorage().getApiUrl(GET_CONTACT_DETAIL_BATCH) +
      "access_token=" + mainService.getWxCpTpConfigStorage().getAccessToken(corpId);
    JsonObject json = new JsonObject();
    json.add("userid_list", new Gson().toJsonTree(userIdList).getAsJsonArray());
    if (StringUtils.isNotBlank(cursor)) {
      json.addProperty("cursor", cursor);
    }
    if (limit != null) {
      json.addProperty("limit", limit);
    }
    String responseContent = this.mainService.post(url, json.toString());
    return WxCpExternalContactBatchInfo.fromJson(responseContent);
  }
}
